package com.zzy.furns.dao.impl;

import com.zzy.furns.dao.BasicDAO;
import com.zzy.furns.dao.MemberDAO;
import com.zzy.furns.domain.Member;


public class MemberDAOImpl extends BasicDAO<Member> implements MemberDAO {

    /**
     * 根据userName查询Member
     * @param userName 用户名
     * @return 对应userName的Member
     */
    @Override
    public Member queryMemberByUsername(String userName) {
        String sql = "SELECT * FROM member WHERE username = ?";
        Member member = querySingle(sql, Member.class, userName);
        return member;
    }

    /**
     * 保存传入的Member对象
     * @param member 保存的Member对象
     * @return 数据库受影响的行数
     */
    @Override
    public int saveMember(Member member) {
        String sql = "INSERT INTO member(username,`password`,email) VALUES(?,MD5(?),?)";
        int affectedRow = update(sql, member.getUsername(), member.getPassword(), member.getEmail());
        return affectedRow;
    }

    /**
     * 根据用户名和密码查询Member
     * @return
     */
    @Override
    public Member queryMemberByUsernameAndPassword(String username,String password) {
        String sql = "SELECT id,username,`password`,email FROM member WHERE username = ? AND `password` = MD5(?)";
        Member member = querySingle(sql, Member.class, username, password);
        return member;
    }
}
